<?php

class Application_Model_HistoCarteElement
{
	protected $id;
	protected $client;		//objet Application_Model_User
	protected $montant;
	protected $time;
	protected $commentaire;
	protected $verif;
	protected $vendeur;

	public function Application_Model_HistoCarteElement($client,$vendeur, $montant=0,$comment,$time=null,$verif=0,$id=0) //client peut être passés comme instance de Application_Model_User ou par login
	{

		if(!is_string($client)) {
			$this->client = $client;
		} else {
			$a = Application_Model_User::getByLogin($client);
			$this->client = $a;
		}

		if(!is_string($vendeur)) {
			$this->vendeur = $vendeur;
		} else {
			$a = Application_Model_User::getByLogin($vendeur);
			$this->vendeur = $a;
		}

		$this->montant = $montant;
		$this->commentaire = $comment;
		if(isset($time)) {
			$this->time = $time;
		} else {
			$this->time = date('Y-m-d H:i:s');
		}

		$this->verif = $verif;
		$this->id = $id;
	}

	public function save()
	{
		$db = new Application_Model_HistoCarteTable();
		$data = array(
		  'login' => $this->client->getLogin(),
		  'montant' => $this->montant,
		  'date' => $this->time,
		  'commentaire' => $this->commentaire,
		  'verif' => $this->verif,
		  'login_vendeur' => $this->vendeur->getLogin(),
		);
		$db->insert($data);
	}

	public function getID()
	{
		return $this->id;
	}

	public function getClient()
	{
		return $this->client;
	}

	public function getVendeur()
	{
		return $this->vendeur;
	}

	public function getMontant()
	{
		return $this->montant;
	}

	public function getVerif()
	{
		return $this->verif;
	}


	public function getTime()
	{
		return $this->time;
	}

	public function getCommentaire()
	{
		return $this->commentaire;
	}

	public static function getAll() 	//retourne tous les élements de l'historique Vente, dans un tableau
	{
		$db = new Application_Model_HistoCarteTable();
		$rows = $db ->fetchAll();
		$rep = array();
		foreach ($rows as $r)
		{
			$objet = new self($r['login'],$r['login_vendeur'], $r['montant'], $r['commentaire'], $r['date'], $r['verif'],$r['id_action_carte']);
			$rep[] = $objet;
		}
		return $rep;
	}

	public static function getByClient($client)
	{
		if(!is_string($client))
		{
			$login = $client->getLogin();
		}
		else
		{
			$login = $client;
		}
		$db = new Application_Model_HistoCarteTable();
		$rows = $db->fetchAll($db->select()->where('login = ?',$login));
		$rep = array();
		foreach ($rows as $r)
		{
			$objet = new self($r['login'],$r['login_vendeur'], $r['montant'], $r['commentaire'],$r['date'],$r['verif'],$r['id_action_carte']);
			$rep[] = $objet;
		}
		return $rep;
	}

	//fonction getByIdTransac à faire
	public static function getByIdTransac($id_transac)
	{
		$db = new Application_Model_HistoCarteTable();
		$row = $db->fetchAll($db->select()->where('id_action_carte = ?',$id_transac));
		if(count($row) > 0)
		{
			$r = $row[0];
			$rep = new self($r['login'],$r['login_vendeur'], $r['montant'], $r['commentaire'],$r['date'],$r['verif'],$r['id_action_carte']);
			return $rep;
		}
		else
		{
			throw new Zend_Exception('Transaction introuvable');
		}
	}

	//fonction getBetweenDates($date1,$date2,$verif=false,$noverif=true) à faire
	//cette fonction renvoie le tableau des éléments d'histo carte entre les deux dates qui sont vérifiés si $verif est à true, qui ne le sont pas si $noverif est à true et tous les éléments si $verif et $noverif sont à true tous les deux
	public static function getBetweenDates($date1,$date2,$verif=0,$noverif=1)
	{
		$db = new Application_Model_HistoCarteTable();
		$rep = array();
		$repdb = array();
		if($noverif && !$verif)
		{
			$repdb = $db->fetchAll($db->select()
			->where('date >= ?',$date1)
			->where('date <= ?',$date2)
			->where('verif = ?','FALSE')
			->order('date'));
		}
		else if(!$noverif && $verif)
		{
			$repdb = $db->fetchAll($db->select()
			->where('date >= ?',$date1)
			->where('date <= ?',$date2)
			->where('verif = ?','TRUE')
			->order('date'));
		}
		else if($noverif && $verif)
		{
			$repdb = $db->fetchAll($db->select()
			->where('date >= ?',$date1)
			->where('date <= ?',$date2)
			->order('date'));
		}
		else
		{
			throw new Zend_Exception('Cochez au moins l\'une des deux cases.');
		}
		if(count($repdb)>0)
		{
			foreach($repdb as $r)
	  {
	  	$rep[] = new self($r['login'],$r['login_vendeur'],$r['montant'],$r['commentaire'],$r['date'],$r['verif'],$r['id_action_carte']);
	  }
	  return $rep;
		}
		else
		{
			throw new Zend_Exception('Aucune transaction ne correspond à votre recherche.');
		}
	}

	public function changeVerif()
	{
		if($this->verif)
		{
			$this->verif = 0;
			$data = array('verif' => 'FALSE');
		}
		else
		{
			$this->verif = 1;
			$data = array('verif' => 'TRUE');
		}
		$where = array('id_action_carte = ?' => $this->getID());
		$db = new Application_Model_HistoCarteTable();
		$db->update($data,$where);
		return $this;
	}
}